package com.aaa.controller;

import com.aaa.mapper.ClazzMapper;
import com.aaa.pojos.Clazz;
import com.aaa.pojos.PackBean;
import com.aaa.pojos.Result;
import com.aaa.pojos.Student;
import com.aaa.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    //p2修改
    //p1修改
    @Resource
    private ClazzService clazzService;
    @Autowired
    private ClazzMapper clazzMapper;

    @GetMapping
    public Result queryName(String name,
               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
               @RequestParam(defaultValue = "1") int page,
               @RequestParam(defaultValue = "10")  int pageSize){
        log.info("接收数据"+name+begin+end+page+pageSize);
        PackBean<Clazz> packBean = clazzService.queryName(name,begin,end,page,pageSize);
        log.info("packBean{}"+packBean);
        return Result.success(packBean);
    }


    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz){
//       log.info(String.valueOf(clazz));
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryId(@PathVariable("id") Integer id){
//        log.info("获取的id"+id);
         Clazz clazz = clazzService.queryId(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("需要修改的参数"+clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable("id") Integer id){
//        log.info("id是"+id);
      Boolean a =  clazzService.deleteClazz(id);
      if (a == true){
          return Result.success();
      }else {
          return Result.error("该班级下有学员");
      }
    }
    @GetMapping("/list")
    public Result queryAllName(){
        List<Clazz> clazz= clazzService.queryAll();
        return Result.success(clazz);
    }


}
